﻿using BusinessLogicLayer;
using ModelLayer;
using ModelLayer.DTO;
using ModelLayer.Graph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DataAccessLayer;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            //p.InsertStuff2();
            var g = p.GrabGraph();
            Task.WaitAll(g);
            //Console.WriteLine(g.Result.ToString());
            p.WritePath(g.Result.GetShortestRoute(Edge.WeightType.DISTANCE));
            Console.ReadLine();
            Console.ReadKey();
        }

        public void SendEmail(string mail)
        {
            AccountManager uam = new AccountManager();
            uam.SendConformationEmail(new UserAccount { email = mail });
        }

        public async Task<StationNetworkGraph> GrabGraph()
        {
            ReservationManager rm = new ReservationManager();
            return await rm.GetStationGraph(new double[] { 55.6761, 12.5683 }, new double[] { 57.0500, 9.9167 }, new BatteryType() { id = 0 },
                new UserAccount() { id = 2, session_token = "qwer1234566" });
        }

        public void WritePath(LinkedList<StationNode> nodes)
        {
            string path = "";
            foreach(StationNode node in nodes)
            {
                if(node.Station == null)
                {
                    path += " non station point ";
                }
                else
                {
                    path += node.Station.name + "  =>  ";
                }
            }
            Console.WriteLine(path);
        }



        //public async void  DoSth()
        //{
        //    ReservationManager rm = new ReservationManager();
        //    BatteryType bat_type = rm.GetAllBatteryTypes().Where(bt => bt.id == 1).First();
        //    Console.WriteLine("Bat id: " + bat_type.id + " bat max dist: " + bat_type.maximum_distance);
            
        //    StationNetworkGraph graph = await rm.GetStationGraph(new double[] { 20, 30 }, new double[] { 56.512065, 9.949950 }, bat_type, 4);
        //    Console.WriteLine(graph.ToString());

            
        //    Console.ReadLine();
        //}

        public void RemoveStationCheck()
        {
            StationManager sm = new StationManager();
            sm.RemoveStation(new Station() { id = 16 });
        }

        //public void CancelReservation()
        //{
        //    Thread.Sleep(5000);
        //    ReservationManager rm = new ReservationManager();
        //    rm.CancelReservation(new RouteReservation() { id = 0 });
        //}

        public void InsertStuff()
        {
            ReservationManager rm = new ReservationManager();
            rm.InsertStuff();
        }

        public void InsertStuff2()
        {
            ReservationManager rm = new ReservationManager();
            rm.InsertStuff2();
        }

        //public void MakeThreadedDelete()
        //{
        //    ThreadStart ts1 = new ThreadStart(MakeReservation);
        //    Thread t1 = new Thread(ts1) { Name = "1" };
        //    ThreadStart ts2 = new ThreadStart(CancelReservation);
        //    Thread t2 = new Thread(ts2) { Name = "2" };

        //    t1.Start();
        //    //t2.Start();
        //}

        public void MakeThereadedReservation()
        {
            ThreadStart ts1 = new ThreadStart(MakeReservation);
            Thread t1 = new Thread(ts1)  { Name = "1" };
            ThreadStart ts2 = new ThreadStart(MakeReservation2);
            Thread t2 = new Thread(ts2) { Name = "2" };

            t1.Start();
            t2.Start();

        }

     

        public void MakeReservation()
        {
            //ReservationManager rm = new ReservationManager();
            //UserAccount ua = new UserAccount() { id = 2 };
            //BatteryType batteryType = rm.GetAllBatteryTypes().Where(bt => bt.id == 1).First();
            //IList<PointReservationDTO> points = new List<PointReservationDTO>();
            //for (int i = 1; i < 5; i++)
            //{
            //    DateTime reservationDate = new DateTime(2014, 10, i);

            //    PointReservationDTO prdto = new PointReservationDTO() { StationId = i, Distance = 40, ReservationTime = reservationDate };
            //    points.Add(prdto);
            //}

            //Console.WriteLine("Route reserved: " + rm.TryToReserveRoute(points, batteryType, ua) + " Thread: " + Thread.CurrentThread.Name);
        }

        public void MakeReservation2()
        {
            //Console.WriteLine("Thread: " + Thread.CurrentThread.Name + " is sleeping for 3 sec before entering.");
            //Thread.Sleep(3000);
            //ReservationManager rm = new ReservationManager();
            //UserAccount ua = new UserAccount() { id = 2 };
            //BatteryType batteryType = rm.GetAllBatteryTypes().Where(bt => bt.id == 2).First();
            //IList<PointReservationDTO> points = new List<PointReservationDTO>();
            //for (int i = 1; i < 5; i++)
            //{
            //    DateTime reservationDate = new DateTime(2014, 10, i);

            //    PointReservationDTO prdto = new PointReservationDTO() { StationId = i, Distance = 40, ReservationTime = reservationDate };
            //    points.Add(prdto);
            //}

            //Console.WriteLine("Route reserved: " + rm.TryToReserveRoute(points, batteryType, ua) + " Thread: " + Thread.CurrentThread.Name);
        }

    }
}
